﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:DetectResult
	/// </summary>
	public partial class DetectResult
	{
        
		public DetectResult()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		    return SQLiteHelper.GetMaxID("reactionCupID", "DetectResult"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int reactionCupID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DetectResult");
			strSql.Append(" where reactionCupID=@reactionCupID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@reactionCupID", DbType.Int32,4)};
			parameters[0].Value = reactionCupID;
			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.DetectResult model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DetectResult(");
            strSql.Append("TestTubeID,TestTubeBarcode,SampleID,DetectItem,result,ResultState,description,checkDatetime,lightValue,note)");
            strSql.Append(" values (");
            strSql.Append("@TestTubeID,@TestTubeBarcode,@SampleID,@DetectItem,@result,@ResultState,@description,@checkDatetime,@lightValue,@note)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@TestTubeID", DbType.Int64),
					new SQLiteParameter("@TestTubeBarcode", DbType.String,50),
					new SQLiteParameter("@SampleID", DbType.Int64),
					new SQLiteParameter("@DetectItem", DbType.String,20),
					new SQLiteParameter("@result", DbType.String,20),
					new SQLiteParameter("@ResultState", DbType.Int32),
					new SQLiteParameter("@description", DbType.String,50),
					new SQLiteParameter("@checkDatetime", DbType.DateTime),
					new SQLiteParameter("@lightValue", DbType.Int32),
					new SQLiteParameter("@note", DbType.String,255)};
            parameters[0].Value = model.TestTubeID;
            parameters[1].Value = model.TestTubeBarcode;
            parameters[2].Value = model.SampleID;
            parameters[3].Value = model.DetectItem;
            parameters[4].Value = model.result;
            parameters[5].Value = model.ResultState;
            parameters[6].Value = model.description;
            parameters[7].Value = model.checkDatetime;
            parameters[8].Value = model.lightValue;
            parameters[9].Value = model.note;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.DetectResult model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DetectResult set ");
            strSql.Append("TestTubeID=@TestTubeID,");
            strSql.Append("TestTubeBarcode=@TestTubeBarcode,");
            strSql.Append("SampleID=@SampleID,");
            strSql.Append("DetectItem=@DetectItem,");
            strSql.Append("result=@result,");
            strSql.Append("ResultState=@ResultState,");
            strSql.Append("description=@description,");
            strSql.Append("checkDatetime=@checkDatetime,");
            strSql.Append("lightValue=@lightValue,");
            strSql.Append("note=@note");
            strSql.Append(" where reactionCupID=@reactionCupID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@TestTubeID", DbType.Int64),
					new SQLiteParameter("@TestTubeBarcode", DbType.String,50),
					new SQLiteParameter("@SampleID", DbType.Int64),
					new SQLiteParameter("@DetectItem", DbType.String,20),
					new SQLiteParameter("@result", DbType.String,20),
					new SQLiteParameter("@ResultState", DbType.Int32),
					new SQLiteParameter("@description", DbType.String,50),
					new SQLiteParameter("@checkDatetime", DbType.DateTime),
					new SQLiteParameter("@lightValue", DbType.Int32),
					new SQLiteParameter("@note", DbType.String,255),
					new SQLiteParameter("@reactionCupID", DbType.Int32)};
            parameters[0].Value = model.TestTubeID;
            parameters[1].Value = model.TestTubeBarcode;
            parameters[2].Value = model.SampleID;
            parameters[3].Value = model.DetectItem;
            parameters[4].Value = model.result;
            parameters[5].Value = model.ResultState;
            parameters[6].Value = model.description;
            parameters[7].Value = model.checkDatetime;
            parameters[8].Value = model.lightValue;
            parameters[9].Value = model.note;
            parameters[10].Value = model.reactionCupID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetStardList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reactionCupID,TestTubeID,TestTubeBarcode,SampleID,DetectItem,result,ResultState,description,checkDatetime,lightValue,note ");
            strSql.Append(" FROM DetectResult ");

            strSql.Append("order by reactioncupid desc limit 6");

            return SQLiteHelper.Query(strSql.ToString());
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int reactionCupID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectResult ");
			strSql.Append(" where reactionCupID=@reactionCupID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@reactionCupID", DbType.Int32,4)			};
			parameters[0].Value = reactionCupID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string reactionCupIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectResult ");
			strSql.Append(" where reactionCupID in ("+reactionCupIDlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectResult GetModel(int reactionCupID)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reactionCupID,TestTubeID,TestTubeBarcode,SampleID,DetectItem,result,ResultState,description,checkDatetime,lightValue,note from DetectResult ");
            strSql.Append(" where reactionCupID=@reactionCupID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@reactionCupID", DbType.Int32,4)
			};
            parameters[0].Value = reactionCupID;

			Clipper.Model.DetectResult model=new Clipper.Model.DetectResult();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectResult DataRowToModel(DataRow row)
		{
			Clipper.Model.DetectResult model=new Clipper.Model.DetectResult();
			if (row != null)
			{
				if(row["reactionCupID"]!=null && row["reactionCupID"].ToString()!="")
				{
					model.reactionCupID=int.Parse(row["reactionCupID"].ToString());
				}
				if(row["TestTubeID"]!=null && row["TestTubeID"].ToString()!="")
				{
					model.TestTubeID=long.Parse(row["TestTubeID"].ToString());
				}
                if (row["TestTubeBarcode"] != null)
                {
                    model.TestTubeBarcode = row["TestTubeBarcode"].ToString();
                }
				if(row["SampleID"]!=null && row["SampleID"].ToString()!="")
				{
					model.SampleID=long.Parse(row["SampleID"].ToString());
				}
				if(row["DetectItem"]!=null)
				{
					model.DetectItem=row["DetectItem"].ToString();
				}
				if(row["result"]!=null)
				{
					model.result=row["result"].ToString();
				}
				if(row["ResultState"]!=null && row["ResultState"].ToString()!="")
				{
					model.ResultState=int.Parse(row["ResultState"].ToString());
				}
				if(row["description"]!=null)
				{
					model.description=row["description"].ToString();
				}
				if(row["checkDatetime"]!=null && row["checkDatetime"].ToString()!="")
				{
					model.checkDatetime=DateTime.Parse(row["checkDatetime"].ToString());
				}
				if(row["lightValue"]!=null && row["lightValue"].ToString()!="")
				{
					model.lightValue=int.Parse(row["lightValue"].ToString());
				}
				if(row["note"]!=null)
				{
					model.note=row["note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reactionCupID,TestTubeID,TestTubeBarcode,SampleID,DetectItem,result,ResultState,description,checkDatetime,lightValue,note ");
            strSql.Append(" FROM DetectResult ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM DetectResult ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.reactionCupID desc");
			}
			strSql.Append(")AS Row, T.*  from DetectResult T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "DetectResult";
			parameters[1].Value = "reactionCupID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

